/**
 * 
 */
package pl.edu.agh.to;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Iterator;

/**
 * @author karulis
 * 
 */
public class ScheduleItem implements Iterator<TimeIntervals> {

	private final Integer picesCount;
	private Integer curentPice = 0;

	/**
	 * @return the courentPice
	 */
	public Integer getCurentPice() {
		return curentPice;
	}

	private final String day;
	private final TimeIntervals start;
	private final TimeIntervals courent;
	private final TimeIntervals stop;
	private final Course course;
	final Integer quoter = 15;

	/**
	 * @param course
	 * @param stop
	 * @param start
	 * @param day
	 * 
	 */
	public ScheduleItem(String day, String start, String stop, Course course) {
		this.day = day;
		this.course = course;
		this.start = new TimeIntervals(start);
		this.stop = new TimeIntervals(stop);
		this.courent = new TimeIntervals();
		picesCount = TimeIntervals.intervals(this.start, this.stop);
	}

	public Integer getPicesCount() {
		return picesCount;
	}

	public String getDay() {
		return day;
	}

	public Course getCourse() {
		return course;
	}

	@Override
	public boolean hasNext() {
		return curentPice < picesCount;
	}

	@Override
	public TimeIntervals next() {
		curentPice++;
		if (courent.getHour() < 0) {
			courent.set(start);
		} else {
			courent.add(quoter);
		}
		return courent;
	}

	@Override
	public void remove() {
		throw new UnsupportedOperationException();
	}

	@Override
	public String toString() {
		return start + " - " + stop + "     " + course;
	}
}

/**
 * @author karulis
 * 
 */
class TimeIntervals {
	private Integer minute;
	private Integer hour;

	public TimeIntervals(String start) {
		this.minute = new Integer(start.split(":")[1]);
		this.hour = new Integer(start.split(":")[0]);
	}

	public TimeIntervals() {
		this.minute = new Integer(-1);
		this.hour = new Integer(-1);
	}

	/**
	 * @return the hour
	 */
	public Integer getHour() {
		return hour;
	}

	public void set(TimeIntervals start) {
		minute = start.getMinute();
		hour = start.getHour();
	}

	public void add(Integer quoter) {
		minute += quoter;
		if (minute >= 60) {
			minute = 0;
			hour++;
		}
	}

	/**
	 * @return the minute
	 */
	public Integer getMinute() {
		return minute;
	}

	@Override
	public boolean equals(Object o) {
		return (getHour().equals(((TimeIntervals) o).getHour()) && getMinute()
				.equals(((TimeIntervals) o).getMinute()));
	}

	public static Integer intervals(TimeIntervals start, TimeIntervals stop) {
		return (stop.getHour() - start.getHour()) * 4
				+ (stop.getMinute() - start.getMinute()) / 15;
	}

	@Override
	public String toString() {
		NumberFormat format = new DecimalFormat("00");
		return format.format(hour) + ":" + format.format(minute);
	}
}
